package week10;

import java.util.Scanner;

class Repo
{
	private int size;
	private String[] str;
	private int i=0;
	private boolean flag=true;
	public Repo(String item)
	{
			this.str=item.split("\\s+");
			this.size=str.length;
	}
	public int getSize()
	{
		return size-i;
	}
	public synchronized void rewu1(){
		while(getSize()>0)

		{
			if(flag==true)
			{
				try
				{
					wait();
				} catch (InterruptedException e)  
				{  
				}  
			}
			if(i<str.length)
			{
				System.out.println(Thread.currentThread().getName()+" finish "+str[i++]);
				flag=true;
				notifyAll();
			}
			
		}			
	}
	
	public synchronized void rewu2(){
		while(getSize()>0)

		{
			if(flag==false)
			{
				try
				{
						wait();
				} catch (InterruptedException e)  
					{  
					}  
				}
			if(getSize()>0)

			{	
				System.out.println(Thread.currentThread().getName()+" finish "+str[i++]);
				flag=false;
				notifyAll();
			}				
		}		
	}
}

class Worker1 implements Runnable{
	private Repo repo;
	public Worker1(Repo repo)
	{
		this.repo=repo;
	}
	public synchronized void run()
	{
		repo.rewu1();
	}
}

class Worker2 implements Runnable
{
	private Repo repo;
	public Worker2(Repo repo)
	{
		this.repo=repo;
	}
	public synchronized void run()
	{
		repo.rewu2();
	}
	
}
public class foursix {

	public static void main(String[] args)  throws InterruptedException {
	
       
        Scanner sc = new Scanner(System.in);
        Repo repo = new Repo(sc.nextLine());
        Thread t1 = new Thread(new Worker1(repo));
        Thread t2 = new Thread(new Worker2(repo));
        t1.start();
        Thread.yield();
        t2.start();
        sc.close();

	}

}
